/**
 * Created by WT-SUN on 2017/4/24.
 */
$(function () {
    $('#btn_save').on('click', function () {
        submitForm();
    });
    /**
     * 绑定监测单位选择事件
     */
    $("#serviceId").textbox({
        iconWidth: 20,
        icons: [{
            iconCls: 'icon-add',
            handler: function(e){
                openSerViceTb();
            }
        },{
            iconCls: 'icon-remove',
            handler: function(e){
                $(e.data.target).textbox('clear');
            }
        }]
    });
    $("#diseasesNameId").textbox({
        iconWidth: 20,
        icons: [{
            iconCls: 'icon-add',
            handler: function(e){
                add_dis_func();
            }
        },{
            iconCls: 'icon-remove',
            handler: function(e){
                $(e.data.target).textbox('clear');
                $("#leibie").textbox('setValue', '').textbox('setText', '');
            }
        }]
    });
    var isDiagnosis=[{id:'0',text:'是',selected:true},{id:'1',text:'否'}];
    $("#isDiagnosis").combobox({
        data: isDiagnosis,
        valueField: 'id',
        textField: 'text',
        editable: false,
        panelHeight: '200',
        selectOnNavigation:true,
        onSelect:function (r) {
            if (r.id=='0'){
                $("#zdFile").show();
            }else {
                $("#zdFile").hide();
            }
        }
    });
    pageComData('isAppraisal',isDiagnosis);
    var OHE_HEALTH_ACSTATEZD=CONSTANT.DictionaryTypeDefConstants.OHE_HEALTH_ACSTATEZD;//状态
    var OHE_HEALTH_ACZGTYPE=CONSTANT.DictionaryTypeDefConstants.OHE_HEALTH_ACZGTYPE;//转归类型
    queryComboData(OHE_HEALTH_ACSTATEZD,'stateZd');
    queryComboData(OHE_HEALTH_ACZGTYPE,'zgType');
    /**
     * 绑定人员选择事件
     */
    $("#userId").textbox({
        iconWidth: 20,
        icons: [{
            iconCls: 'icon-add',
            handler: function(e){
                add_userId_func();
            }
        },{
            iconCls: 'icon-remove',
            handler: function(e){
                $(e.data.target).textbox('clear');
                $("#danwei").textbox('setValue', '').textbox('setText', '');
                $("#gonghao").textbox('setValue', '').textbox('setText', '');
                $("#gangwei").textbox('setValue', '').textbox('setText', '');
                $("#xingbie").textbox('setValue', '').textbox('setText', '');
                $("#chusheng").textbox('setValue', '').textbox('setText', '');
            }
        }]
    });
});

function add_dis_func() {
    WindowManager.openLayer({
        type: 2,
        title: '选择职业病',
        area: ['850px', '70%'],
        content: ROOT_PATH + '/occupationalHealth/occDiseases/disTb.shtml',
        callBack:function(data){
            getSelectedDis(data);
        }
    });
}
function getSelectedDis(data) {
    $("#diseasesNameId").textbox('setValue', data[0].id).textbox('setText', data[0].diseasesName);
    $("#leibie").textbox('setValue', data[0].diseasesTypeName).textbox('setText', data[0].diseasesTypeName);
}

function openSerViceTb() {
    WindowManager.openLayer({
        type: 2,
        title: '选择服务机构',
        area: ['850px', '70%'],
        content: ROOT_PATH + '/occupationalHealth/hseServiceOrg/orgTb.shtml',
        callBack:function(data){
            getSelectedOrg(data);
        }
    });
}
function getSelectedOrg(data) {
    $("#serviceId").textbox('setValue', data[0].id).textbox('setText', data[0].serviceName);
}
var add_userId_func = function () {
    WindowManager.openLayer({
        type: 2,
        title: '选择人员信息',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
        callBack:function(data){
            getSelectedUser(data);
        }
    });
};

/**
 * 人员信息选择
 */
var getSelectedUser = function (data) {
    $("#userId").textbox('setValue', data[0].id).textbox('setText', data[0].realName);
    var deptName='';
    if (data[0].userDepartmentList && data[0].userDepartmentList.length > 0) {
        for (var index in data[0].userDepartmentList) {
            if (data[0].userDepartmentList[index].deptName != null) {
                deptName += ',' + data[0].userDepartmentList[index].deptName;
            }
        }
        deptName= deptName.substring(1);
    } else {
        deptName='';
    }
    $("#danwei").textbox('setValue', deptName).textbox('setText', deptName);
    $("#gonghao").textbox('setValue', data[0].code).textbox('setText', data[0].code);
    $("#gangwei").textbox('setValue', data[0].postName).textbox('setText', data[0].postName);
    if (data[0].userInfo!=null){
        var gender='';
        if (data[0].userInfo.gender=='1'){
            gender='男'
        }
        if (data[0].userInfo.gender=='0'){
            gender='女'
        }
        $("#xingbie").textbox('setValue', gender).textbox('setText', gender);
        $("#chusheng").textbox('setValue', data[0].userInfo['birthday']).textbox('setText', data[0].userInfo['birthday']);
    }
};
function submitForm() {
    $('#formAccount').form('submit', {
        url: ROOT_PATH + '/occupationalHealth/account/insertSelective.shtml',
        onSubmit: function (param) {
            if (!$(this).form('validate')) {
                layer.alert('请正确填写信息', {icon: 5});
            }
            return $(this).form('validate');
        },
        success: function (result) {
            result = $.parseJSON(result);
            if (result.success) {
                layer.msg('保存成功', {
                    icon: 1,
                    time: 1000
                }, function () {
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                    $.getIframeWindow().reLoadData();
                });
            }
            else {
                layer.msg('保存失败', {
                    icon: 2,
                    time: 1000
                });
            }
        }
    });
}
function pageComData(id,data) {
    var pageId='#'+id;
    $(pageId).combobox({
        data: data,
        valueField: 'id',
        textField: 'text',
        editable: false,
        panelHeight: '200',
        selectOnNavigation:true,
    });
}
function queryComboData(code,queryId) {
    /*var EMPTY_OPTION = {
        id: '',
        text: '请选择'
    };*/
    var pageId='#'+queryId;
    $.ajax({
        type: 'post',
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml',
        data: {code: code},
        dataType: "json",
        success: function (result) {
            $(pageId).combobox({
               /* loadFilter: function (data) {
                    data.unshift(EMPTY_OPTION);
                    return data;
                },*/
                data: result,
                prompt:'请选择',
                valueField: 'id',
                textField: 'text',
                editable: false,
                panelHeight: '200',
            });
        }
    });
}
